<?php
class BlogComments extends CActiveRecord {
    
    public $artId;
    public $userId;
    public $text;
    public $date;
    
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
    
    public function beforeValidate() {
        if(!parent::beforeValidate()) return false;
        $this->attributes = !empty($_POST[get_class($this)]) ? $_POST[get_class($this)] : array();
        return true;
    }
    
    public function rules() {
        return array(
            array('text', 'required', 'message' => '{attribute} не может быть пустым'),
        );
    }

    public function tableName() {
        return 'blog_comments';
    }
    
    public function beforeSave() {
        if(!parent::beforeSave()) return false;
        $this->date = time();
        $this->userId = Yii::app()->user->isGuest ? 0 : Yii::app()->user->getId() ;
        return true;
    }

    public function pagination($limit, $offset) {
        $this->getDbCriteria()->mergeWith(array(
            'offset' => $offset,
            'limit' => $limit,
        ));
        return $this;
    }
}
